// 手写 promise
class MyPromise {
    constructor(executor) {
        const PROMISE_STATUS_PENDING = 'pending' //定义三种状态常量
        const PROMISE_STATUS_FULFILLED = 'fulfilled'
        const PROMISE_STATUS_REJECTED = 'rejected'
        this.status = PROMISE_STATUS_PENDING
        this.value = undefined
        this.error = undefined

        const reverse = (value) => {
            if (this.status === PROMISE_STATUS_PENDING) {
                if (this.status !== PROMISE_STATUS_PENDING) return
                queueMicrotask(() => {
                    this.status = PROMISE_STATUS_FULFILLED
                    this.value = value
                    this.resfn(value)
                })
            }
        }

        const reject = (error) => {
            if (this.status === PROMISE_STATUS_PENDING) {
                if (this.status !== PROMISE_STATUS_PENDING) return
                queueMicrotask(() => {
                    this.status = PROMISE_STATUS_REJECTED
                    this.error = error
                    this.errfn(error)
                })
            }
        }

        executor(reverse, reject)
    }

    then(res, rej) {
        this.resfn = res
        this.errfn = rej
    }
}

const api = new MyPromise((res, rej) => {
    res('正确')
    rej('错误')
})

api.then(res => {
    console.log(res, '47')
})

// 冒泡排序
const arr = [1, 88, 55, 16, 4, 56, 23, 45, 8, 6, 65, 89, 5, 5, 5, 5, 89, 56]
const arr1 = [1, 88, 55, 16, 4, 56, 23, 45, 8, 6, 65, 89, 5, 5, 5, 5, 89, 57,]
const handleArr = (arr) => {
    for (let i = 0; i < arr.length; i++) {
        for (let j = 0; j < arr.length; j++) {
            if (arr[j] > arr[j + 1]) {
                [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]
            }
        }
    }
    console.log(arr)
}
handleArr(arr)
const setArr = [...new Set(arr)]
console.log(setArr, '去重')

const deepclone = function () {
    
}
